﻿using Data;
using Entities;
using System.Collections.Generic;
using System;
using System.Threading;

namespace Logic
{
    public sealed class StatisticLogic
    {

        public void InsertStatisticLogic(List<Candidate> candidateList, Voter voter, dynamic votingId, string key)
        {
            List<Statistic> listStatistic = new List<Statistic>();

            votingId = DecryptDataLogic(votingId, key);

            foreach (Candidate cc in candidateList)
            {
                Statistic s = new Statistic();
                if (string.IsNullOrWhiteSpace(cc.id))
                {
                    s.candidateWhite = true;
                    s.candidateId = 0;
                }
                else
                {
                    s.candidateWhite = false;
                    s.candidateId = Convert.ToInt32(cc.id);
                }

                s.votingId = Convert.ToInt32(votingId);
                s.regionId = Convert.ToInt32(voter.regionId);
                s.birthDate = voter.birthDate;
                s.civilStatusId = Convert.ToInt32(voter.civilStatus);
                s.communeId = Convert.ToInt32(voter.communeId);
                s.sexId = Convert.ToInt32(voter.sex);
                s.register = DateTime.Now;
                listStatistic.Add(s);
            }

            Thread insert = new Thread(delegate() { new StatisticData().InsertStatistic(listStatistic); });
            insert.IsBackground = true;
            insert.Start();

            // return new StatisticData().InsertStatistic(s);
        }

        public string DecryptDataLogic(string data, string key)
        {
            return Encryption.DecryptData(data, key);
        }

    }
}
